<?php

namespace app\admin\services;

use app\common\model\AppointmentFlow;
use app\common\model\Area;
use app\common\model\Business;
use app\common\model\Member;
use app\common\services\BaseService;
use app\common\util\DateTimeUtil;
use app\common\util\weixin\TemplateMessage;

class AppointmentService extends BaseService
{
    public function __construct()
    {
        $viewName = '(select a.*,area.title area_name,b.title business_name from appointment a
        inner join area on area.id=a.area_id
        inner join business b on b.id=a.business_id
        ) t';
        parent::__construct('', $viewName);
    }

    public function setAuth($data)
    {
        try {
            $id = intval($data['id']);
            $status = intval($data['status']);
            if ($status == 2 && empty($data['reason'])) {
                return [false, '拒绝理由不能为空'];
            }
            $reason = !empty($data['reason']) ? trim($data['reason']) : '';
            $entry = $this->getModelInstance()->where(['id' => $id])->find();
            if (!empty($entry)) {
                if ($entry['status'] == 1) {
                    return [false, '不能再审核已通过的记录'];
                }
                if ($status == 1) { // 审核通过
                    $this->sendWxAuthPassedInfo($id);
                }
                AppointmentFlow::instance()->add([
                    'status' => $status,
                    'content' => $reason,
                    'create_time' => DateTimeUtil::now(),
                    'appointment_id' => $id
                ]);
                $this->getModelInstance()->where(['id' => $id])->setField('status', $status);
                if ($status == 1) { // 审核通过消息
                    $member_id = $entry['member_id'];
                    if (!empty($member_id)) {
                        $openid = Member::instance()->where(['id' => $member_id])->getField('openid');
                        $businessName = Business::instance()->where(['id' => $entry['business_id']])->getField('title');
                        $areaName = Area::instance()->where(['id' => $entry['area_id']])->getField('title');
                        if (!empty($openid) && !empty($businessName) && !empty($areaName)) {
                            //预约成功通知
                            TemplateMessage::sendTemplateMessage([
                                'first' => [
                                    'value' => '您的预约已通过审核'
                                ],
                                'keyword1' => [
                                    'value' => $entry['contact']
                                ],
                                'keyword2' => [
                                    'value' => $entry['telephone']
                                ],
                                'keyword3' => [
                                    'value' => $entry['appoint_date'] . ' ' . $entry['appoint_time']
                                ],
                                'keyword4' => [
                                    'value' => '办理业务《' . $businessName . '》'
                                ],
                                'remark' => [
                                    'value' => "\n".'请按照预约时间前来' . $areaName . '办理，谢谢。',
                                    'color'=>'#173177'
                                ]
                            ], $openid, 'Rz0Zhf50kUQYot_4hBTkluZzR_xIqZMh4E91qYmhKIo',
                                U('weixin/appointment/workResult', [
                                    'id' => $id
                                ], true, true));
                        }
                    }
                } elseif ($status == 2) { //审核拒绝
                    $member_id = $entry['member_id'];
                    if (!empty($member_id)) {
                        $openid = Member::instance()->where(['id' => $member_id])->getField('openid');
                        $businessName = Business::instance()->where(['id' => $entry['business_id']])->getField('title');
                        //申请驳回通知
                        if (!empty($openid) && !empty($businessName)) {
                            TemplateMessage::sendTemplateMessage([
                                'first' => [
                                    'value' => '您预约的业务《' . $businessName . '》未通过审核'
                                ],
                                'keyword1' => [
                                    'value' => '未通过'
                                ],
                                'keyword2' => [
                                    'value' => $reason
                                ],
                                'remark' => [
                                    'value' => "\n".'如有疑问，请致电' . ccnn_sys_setting('客服电话', ''),
                                    'color'=>'#173177'
                                ]
                            ], $openid, 'MH_P8rZKSg9tsaqzJFnksH1WnwQvnooMuIN1NKLPnjE', U('weixin/appointment/workResult', [
                                'id' => $id
                            ], true, true));
                        }
                    }
                }
            }
            return [true, '操作成功'];
        } catch (\Exception $ex) {
            return [false, $ex->getMessage()];
        }
    }

    private function sendWxAuthPassedInfo($id)
    {
        /**
         * {{first.DATA}}
         * 审核类型：{{keyword1.DATA}}
         * 提交内容：{{keyword2.DATA}}
         * 审核时间：{{keyword3.DATA}}
         * {{remark.DATA}}
         */
        $appointment = $this->getModelInstance()->find($id);
        $business_id = $appointment['business_id'];
        $area_id = $appointment['area_id'];
        $member_id = $appointment['member_id'];
        if (!empty($member_id)) {
            $openid = Member::instance()->where(['id' => $member_id])->getField('openid');
            if (!empty($openid)) {
                $business_name = Business::instance()->where(['id' => $business_id])->getField('title');
                $area_name = Area::instance()->where(['id' => $area_id])->getField('title');
                TemplateMessage::sendTemplateMessage([
                    'first' => array('value' => '您好,您在线预约的《' . $business_name . '》项目审核通过,请您于' . $appointment['appoint_date'] . ' ' .
                        $appointment['appoint_time'] . '在' . $area_name . '办理相关事宜'),
                    'keyword1' => array('value' => '业务预约'),
                    'keyword2' => array('value' => '《' . $business_name . '》通过审核'),
                    'keyword3' => DateTimeUtil::now(),
                    'remark' => '如有疑问，请拨打客服热线' . ccnn_sys_setting('客服电话', '') . '，感谢您的支持与信任。'
                ], $openid, 'y_xNKxb-3SGmj0ArUkcxpWYJZzHRAJz0E18YsGglGBQ',
                    U('weixin/appointment/workResult', ['id' => $id], true, true));
            }
        }

    }

}